﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class AdjustingDuctTerminalCommand : HYExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public override Result HYExecute(ref string TranName)
		{
			TranName = "调整风口距离";
			if (AdjustingDuctTerminalCommand.hWndRevit == null)
			{
				AdjustingDuctTerminalCommand.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			AdjustingDuctTerminalPosition adjustingDuctTerminalPosition = new AdjustingDuctTerminalPosition();
			adjustingDuctTerminalPosition.Show(AdjustingDuctTerminalCommand.hWndRevit);
			int wMsg = 7;
			AdjustingDuctTerminalCommand.SendMessage(AdjustingDuctTerminalCommand.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			ISelectionFilter selectionFilter = new DuctAndTerminalFilter();
			Result result;
			try
			{
				for (;;)
				{
					IList<Element> list = base.Sel.PickElementsByRectangle(selectionFilter, "请框选一根风管和风口");
					List<Element> list2 = new List<Element>();
					List<Element> list3 = new List<Element>();
					if (list.Count > 1)
					{
						foreach (Element element in list)
						{
							if (element is Duct)
							{
								list2.Add(element);
							}
							else
							{
								list3.Add(element);
							}
						}
						if (list2.Count > 1 || list2.Count < 1)
						{
							YJKMessageBox.Information("请框选一根风管");
							continue;
						}
						if (list3.Count < 1)
						{
							YJKMessageBox.Information("请框选至少一个风口");
							continue;
						}
						if (list2.Count != 1 || list3.Count < 1)
						{
							continue;
						}
						Duct duct = list2[0] as Duct;
						Line pipeline = (duct.Location as LocationCurve).Curve as Line;
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
						HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
						using (List<Element>.Enumerator enumerator2 = list3.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								Element element2 = enumerator2.Current;
								XYZ point = (element2.Location as LocationPoint).Point;
								XYZ project = hypiping.GetProject(point, false);
								XYZ xyz = new XYZ(point.X, point.Y, project.Z);
								if (!project.IsAlmostEqualTo(xyz))
								{
									XYZ newParallelPipeXYZ = hycreatePipeHelper.getNewParallelPipeXYZ(project, pipeline, double.Parse(adjustingDuctTerminalPosition.tbhori.Text), xyz);
									(element2.Location as LocationPoint).Point = new XYZ(newParallelPipeXYZ.X, newParallelPipeXYZ.Y, newParallelPipeXYZ.Z - double.Parse(adjustingDuctTerminalPosition.tbvert.Text) / 304.8);
								}
								else
								{
									(element2.Location as LocationPoint).Point = new XYZ(project.X, project.Y, project.Z - double.Parse(adjustingDuctTerminalPosition.tbvert.Text) / 304.8);
								}
							}
							continue;
						}
					}
					YJKMessageBox.Information("请框选一根风管和至少一个风口");
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				adjustingDuctTerminalPosition.Close();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception)
			{
				adjustingDuctTerminalPosition.Close();
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public static YJKWindowHandle hWndRevit;
	}
}
